<?php
declare(strict_types=1);
/**
 * MineAdmin is committed to providing solutions for quickly building web applications
 * Please view the LICENSE file that was distributed with this source code,
 * For the full copyright and license information.
 * Thank you very much for using MineAdmin.
 *
 * @Author X.Mo<root@imoi.cn>
 * @Link   https://gitee.com/xmo/MineAdmin
 */

namespace App\Cola\Service;

use App\Cola\Mapper\ColaTradeCateMapper;
use App\Cola\Model\ColaTradeCate;
use Mine\Abstracts\AbstractService;

/**
 * 可乐记账-交易类型服务类
 */
class ColaTradeCateService extends AbstractService
{
    /**
     * @var ColaTradeCateMapper
     */
    public $mapper;

    /**
     * @var ColaTradeCate
     */
    public $model;

    public function __construct(ColaTradeCateMapper $mapper, ColaTradeCate $model)
    {
        $this->mapper = $mapper;
        $this->model = $model;
    }

    /**
     * 获取列表
     * */
    public function getApiList($params, $field = ['*'], $page = 1, $size = 20)
    {
        $query = ColaTradeCate::query()->select($field);
        if (!empty($params['status'])) {
            $query->where('status', $params['status']);
        }

        if (!empty($params['keyword'])) {
            $query->where('name', 'like', "%{$params['keyword']}%");
        }

        $count_query = clone $query;
        $list_query = clone $query;
        $count = $count_query->count();
        $list = $list_query->offset($page)->limit($size)->orderByDesc('weigh')->get();

        return ['count' => $count, 'list' => $list];
    }


}